package com.golddaymicromall.activity.eleaccounts;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.util.AgentUtils;
import com.golddaymicromall.util.ButtonUtils;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class ZhuanZhangZhuanChuActivity extends BaseActivity implements View.OnClickListener {
    private TextView mTv_zhuangzhang;
    private EditText mEt_zhuanzhang;
    private String customerno1;
    private String Et_zhuangzhang;
    private String walletid;
    private String customerid;
    private LinearLayout activity_zhuangzhangzhuangchu_btn_back;
    private String Customerno;
    private String Balance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuan_zhang_zhuan_chu);
        Customerno = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        //获取本地数据
        customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        walletid = getIntent().getStringExtra("walletid");
        Balance = getIntent().getStringExtra("Balance");
        initView();
    }

    private void initView() {
        activity_zhuangzhangzhuangchu_btn_back = (LinearLayout) findViewById(R.id.activity_zhuangzhangzhuangchu_btn_back);
        activity_zhuangzhangzhuangchu_btn_back.setOnClickListener(this);
        customerno1 = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        mTv_zhuangzhang = (TextView) findViewById(R.id.Tv_zhuangzhang);
        mEt_zhuanzhang = (EditText) findViewById(R.id.Et_zhuanzhang);
        mEt_zhuanzhang.setTransformationMethod(PasswordTransformationMethod.getInstance()); //设置为密码输入框

        mTv_zhuangzhang.setText(customerno1);
        Button mSignInButton = (Button) findViewById(R.id.btnZhuanZhang);
        mSignInButton.setOnClickListener(view -> {
            if (ButtonUtils.isFastDoubleClick()) {
                return;
            }
            Et_zhuangzhang = mEt_zhuanzhang.getText().toString();
            if (AgentUtils.isBlank(Et_zhuangzhang)) {
                Toast.makeText(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            attemptChangeDianZi(Et_zhuangzhang);
        });
    }

    private void attemptChangeDianZi(String et) {

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.password.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", customerid);
        form.put("trandword", Et_zhuangzhang);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .doOnSubscribe(() -> showLoading())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    dismissLoading();
                    if (!"".equals(result)) {
                        if (result.contains("errorToken")) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            if ("1.0".equals(result)) {
                                Intent intent = new Intent(ZhuanZhangZhuanChuActivity.this, ZhuangZhangZhuangChuMoneyActivity.class);
                                intent.putExtra("walletid", walletid);
                                intent.putExtra("Balance", Balance);
                                startActivity(intent);
                                finish();
                                MyApplication.getInstance().attemptRiZhi(Customerno, "转为进货电子币密码");
                            } else if (Integer.parseInt(result) == 0) {
                                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
                            } else if (result.contains("2")) {
                                Toast.makeText(getApplicationContext(), "账户异常", Toast.LENGTH_SHORT).show();
                            } else if (Integer.parseInt(result) == 3) {
                                Toast.makeText(getApplicationContext(), "余额不足", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    dismissLoading();
                    showError();
                    throwable.printStackTrace();
                });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_zhuangzhangzhuangchu_btn_back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mEt_zhuanzhang.setText("");
    }
}
